package com.itheima.Demo;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class Demo2 {
    public static void main(String[] args) throws Exception{
        // 读取核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //构建工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        // 获取session对象
        SqlSession session = build.openSession();
        // 通过session获取接口的代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User();
        user.setId(2);
        user.setUsername("lirui");
        List<User> list = mapper.findByName(user);
        System.out.println(list);
        //提交事务
        session.commit();
        //   释放资源
        session.close();
        is.close();
    }
}
